package cn.edu.bjfu.it.oyss.covid_19helper.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
import cn.edu.bjfu.it.oyss.covid_19helper.R;

public class CovidDataBaseHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "covid.db";
    private static final int DB_VERSION = 1;

    public CovidDataBaseHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE PROVINCE(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "NAME TEXT," +
                "ID TEXT," +
                "IMAGE_RESOURCE_ID INTEGER," +
                "RISK NUMERIC);"
        );
        insertProvince(db, "110000", "北京", R.drawable.beijing);
        insertProvince(db, "310000", "上海", R.drawable.shanghai);
        insertProvince(db, "120000", "天津", R.drawable.tianjin);
        insertProvince(db, "130000", "河北", R.drawable.hebeisjz);
        insertProvince(db, "140000", "山西", R.drawable.shanxity);
        insertProvince(db, "150000", "内蒙古", R.drawable.neimenggu);
        insertProvince(db, "210000", "辽宁", 0);
        insertProvince(db, "220000", "吉林", 0);
        insertProvince(db, "230000", "黑龙江", 0);
        insertProvince(db, "320000", "江苏", 0);
        insertProvince(db, "330000", "浙江", 0);
        insertProvince(db, "340000", "安徽", 0);
        insertProvince(db, "350000", "福建", 0);
        insertProvince(db, "360000", "江西", 0);
        insertProvince(db, "370000", "山东", 0);
        insertProvince(db, "410000", "河南", 0);
        insertProvince(db, "420000", "湖北", 0);
        insertProvince(db, "430000", "湖南", 0);
        insertProvince(db, "440000", "广东", 0);
        insertProvince(db, "450000", "广西", 0);
        insertProvince(db, "460000", "海南", 0);
        insertProvince(db, "500000", "重庆", R.drawable.chongqing);
        insertProvince(db, "510000", "四川", 0);
        insertProvince(db, "520000", "贵州", 0);
        insertProvince(db, "530000", "云南", 0);
        insertProvince(db, "540000", "西藏", 0);
        insertProvince(db, "610000", "陕西", 0);
        insertProvince(db, "620000", "甘肃", 0);
        insertProvince(db, "630000", "青海", 0);
        insertProvince(db, "640000", "宁夏", 0);
        insertProvince(db, "650000", "新疆", 0);
        insertProvince(db, "700000", "台湾", 0);
        insertProvince(db, "810000", "香港", 0);
        insertProvince(db, "820000", "澳门", 0);
    }

    private static void insertProvince(SQLiteDatabase db, String id, String name, @Nullable Integer resourceId) {
        ContentValues provinceValues = new ContentValues();
        provinceValues.put("NAME", name);
        provinceValues.put("ID", id);
        provinceValues.put("IMAGE_RESOURCE_ID", resourceId);
        provinceValues.put("RISK", 0);
        long result = db.insert("PROVINCE", null, provinceValues);
        Log.d("sqlite", "insert" + name + "_id:" + result);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}
